package com.gougou.httpclient.web.Api;


import com.gougou.httpclient.service.HttpAPIService;
import com.gougou.httpclient.service.IndexService;
import com.gougou.redis.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/HttpAPI")
public class HttpClientApi {
    final int redisAlivetime =300;

    @Autowired
    private IndexService indexService;

    @Autowired
    private RedisService redisService;

    @Autowired
    private HttpAPIService httpAPIService;

    @Value("${ApiRootURL}")
    private String ApiRootURL;

    /***************************
     *
     * 得到ad1 json
     *
     * @return
     * @throws Exception
     * ************************
     */
    @RequestMapping("/queryIndexAD1")
    public String queryIndexAD1() throws Exception {
        String ad1jsonStr = indexService.queryIndexAD1();
        //装填redis
        redisService.set("ad1jsonStr",ad1jsonStr,redisAlivetime);
        return ad1jsonStr;


    }

    /**************************
     *
     *得到ad2 json
     *
     * @return
     * @throws Exception
     * *************************
     */
    @RequestMapping("/queryIndexAD2")
    public String queryIndexAD2() throws Exception {
        String ad2jsonStr=indexService.queryIndexAD2();
        //装填redis
        redisService.set("ad2jsonStr",ad2jsonStr,redisAlivetime);
        return ad2jsonStr;
    }


    /**************************
     *
     *得到ad3 json
     *
     * @return
     * @throws Exception
     * *************************
     */
    @RequestMapping("/queryIndexAD3")
    public String queryIndexAD3() throws Exception {
        String ad3jsonStr=indexService.queryIndexAD3(null);
        //装填redis
        redisService.set("ad3jsonStr",ad3jsonStr,redisAlivetime);
        return ad3jsonStr;
    }



    @RequestMapping("/item/{id}")
    public String getItemByid(@PathVariable Long id) throws Exception {
        String itemJsonStr = httpAPIService.doGet(ApiRootURL+"/manager/itemApi/item/" + id);
        //装填redis
        redisService.set("item"+id,itemJsonStr,redisAlivetime);
        return itemJsonStr;

    }














}
